#include "ui.h"

int check_space(const char *str)
{
    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            return 1;
        }
    }
    return 0;
}

int login(UserLogInfo *user, Command *cmd_login_reg)
{
    char user_name_input[256];
    char user_password_input[256];
    printf("登录\n");
    printf("请输入用户名(不允许有空格)\n");
    while (1)
    {
        bzero(user_name_input, sizeof(user_name_input));
        fgets(user_name_input, sizeof(user_name_input), stdin);
        user_name_input[strcspn(user_name_input, "\n")] = '\0'; // 去除换行符
        if (check_space(user_name_input))
        {
            printf("错误：输入不能包含空格！请重新输入。\n");
        }
        else
        {
            break;
        }
    }
    memcpy(user->user_name, user_name_input, sizeof(user_name_input));

    printf("密码：\n");
    printf("请输入密码(不允许有空格)\n");
    while (1)
    {
        bzero(user_password_input, sizeof(user_password_input));
        fgets(user_password_input, sizeof(user_password_input), stdin);
        user_password_input[strcspn(user_password_input, "\n")] = '\0'; // 去除换行符
        if (check_space(user_password_input))
        {
            printf("错误：输入不能包含空格！请重新输入。\n");
        }
        else
        {
            break;
        }
    }
    memcpy(user->passward, user_password_input, sizeof(user_password_input));
    *cmd_login_reg = CMD_LOGIN_REQ;

    return 0;
}

// 注册
int register_user(UserLogInfo *user, Command *cmd_login_reg)
{
    char user_name_input[256];
    char user_password1_input[256];
    char user_password2_input[256];
    printf("注册\n");
    printf("请输入用户名(不允许有空格)\n");
    while (1)
    {
        bzero(user_name_input, sizeof(user_name_input));
        fgets(user_name_input, sizeof(user_name_input), stdin);
        user_name_input[strcspn(user_name_input, "\n")] = '\0'; // 去除换行符
        if (check_space(user_name_input))
        {
            printf("错误：输入不能包含空格！请重新输入。\n");
        }
        else
        {
            break;
        }
    }
    memcpy(user->user_name, user_name_input, sizeof(user_name_input));

    while (1)
    {
        printf("密码：\n");
        printf("请输入密码(不允许有空格)\n");
        while (1)
        {
            bzero(user_password1_input, sizeof(user_password1_input));
            fgets(user_password1_input, sizeof(user_password1_input), stdin);
            user_password1_input[strcspn(user_password1_input, "\n")] = '\0'; // 去除换行符
            if (check_space(user_password1_input))
            {
                printf("错误：输入不能包含空格！请重新输入。\n");
            }
            else
            {
                break;
            }
        }
        printf("请再次输入密码(不允许有空格)\n");
        while (1)
        {
            bzero(user_password2_input, sizeof(user_password2_input));
            fgets(user_password2_input, sizeof(user_password2_input), stdin);
            user_password2_input[strcspn(user_password2_input, "\n")] = '\0'; // 去除换行符
            if (check_space(user_password2_input))
            {
                printf("错误：输入不能包含空格！请重新输入。\n");
            }
            else
            {
                break;
            }
        }
        if (strncmp(user_password1_input, user_password2_input, strlen(user_password1_input)) != 0)
        {
            printf("两次密码输入不一致，请重新输入!\n");
            continue;
        }
        else
        {
            break;
        }
    }

    memcpy(user->passward, user_password1_input, sizeof(user_password1_input));
    *cmd_login_reg = CMD_LOGIN_REQ;

    return 0;
}

int display_menu(UserLogInfo *user, Command *cmd_login_reg)
{
    int choice;
    while (1)
    { // 无限循环，直到选择退出
        printf("\n===== 主菜单 =====\n");
        printf("1. 登录\n");
        printf("2. 注册\n");
        printf("3. 退出系统\n");
        printf("请输入您的选择 (1-3): ");

        // 读取用户输入
        if (scanf("%d", &choice) != 1)
        {
            // 如果输入的不是数字，清空输入缓冲区
            while (getchar() != '\n')
                ;
            printf("输入无效，请重新输入！\n");
            continue;
        }

        // 处理用户选择
        switch (choice)
        {
        case 1:
            login(user, cmd_login_reg);
            break;
        case 2:
            register_user(user, cmd_login_reg);
            break;
        case 3:
            printf("\n退出系统,再见！\n");
            exit(0); // 正常退出程序
        default:
            printf("输入无效，请重新输入 1、2 或 3!\n");
        }
    }
}